#include "protocgui.h"
#include "ui_protocgui.h"
#include "qdrag.h"
#include <QDropEvent>
#include <qurl.h>
#include <qdir.h>
#include <qfileinfo.h>
#include <qmessagebox.h>
#include <qprocess.h>
#include <qdebug.h>

ProtocGUI::ProtocGUI(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ProtocGUI)
{
    ui->setupUi(this);

	setAcceptDrops(true);

	connect(ui->compileBtn, SIGNAL(clicked()), this, SLOT(compile()));
}

ProtocGUI::~ProtocGUI()
{
    delete ui;
}

void ProtocGUI::dragEnterEvent(QDragEnterEvent* event)
{
    // if some actions should not be usable, like move, this code must be adopted
    event->acceptProposedAction();
}
 
void ProtocGUI::dragMoveEvent(QDragMoveEvent* event)
{
    // if some actions should not be usable, like move, this code must be adopted
    event->acceptProposedAction();
}
 
 
void ProtocGUI::dragLeaveEvent(QDragLeaveEvent* event)
{
    event->accept();
}

void ProtocGUI::dropEvent(QDropEvent* event)
{
    const QMimeData* mimeData = event->mimeData();
 
    // check for our needed mime type, here a file or a list of files
    if (mimeData->hasUrls())
    {
        QList<QUrl> urlList = mimeData->urls();
 
        // extract the local paths of the files
        for (int i = 0; i < urlList.size() && i < 32; ++i)
        {
            QString file = urlList.at(i).toLocalFile();

			ui->sourceText->setText(file);

			QMessageBox msgBox;
			msgBox.setIcon(QMessageBox::Question);
			msgBox.setText("Set output folder same as source?");
			msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
			msgBox.setDefaultButton(QMessageBox::No);
			if (msgBox.exec() == QMessageBox::Yes)
			{
				QFileInfo f(file);

				ui->outputFolderText->setText(f.absolutePath());
			}
        }

    }
}

void ProtocGUI::compile()
{
	QDir outputDir(ui->outputFolderText->text());
	QFileInfo inputSource(ui->sourceText->text());

	//Now validate the input/output
	if (!outputDir.exists())
	{
		 QMessageBox msgBox;
		 msgBox.setIcon(QMessageBox::Critical);
		 msgBox.setText("Output folder doesn't exists.");
		 msgBox.exec();
		 return;
	}

	if (!inputSource.exists())
	{
		 QMessageBox msgBox;
		 msgBox.setIcon(QMessageBox::Critical);
		 msgBox.setText("Source / .proto file doesn't exists.");
		 msgBox.exec();
		 return;
	}

	if (!QFileInfo("./protoc.exe").exists())
	{
		 QMessageBox msgBox;
		 msgBox.setIcon(QMessageBox::Critical);
		 msgBox.setText("protoc.exe is not reachable, please copy/move it to this app folder.");
		 msgBox.exec();
		 return;
	}
	

	QStringList args;


	QString test = outputDir.absolutePath();

	args << QString().sprintf("-I=%s", inputSource.absolutePath().toStdString().c_str());
	args << QString().sprintf("--cpp_out=%s", outputDir.absolutePath().toStdString().c_str());
	args << inputSource.absoluteFilePath();

	m_pProcess = new QProcess();
	m_pProcess->start("protoc.exe", args);
	m_pProcess->waitForFinished();

	ui->compileResultText->clear();

	if (m_pProcess->exitCode() == 0)
	{
		ui->compileResultText->insertPlainText("Compilation Succeed!");
	}
	else
	{
		ui->compileResultText->insertPlainText("Compilation Failed: \n\n");
		ui->compileResultText->insertPlainText(m_pProcess->readAllStandardError());
	}

	return;
}

void ProtocGUI::readyRead()
{
}